package com.budwk.app.sqfwV2.generator.centuryCity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfPdfGenerator;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityOldCarePdfDTO;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityProblemParamDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.ProblemParamDTO;
import com.budwk.app.sqfwV2.enums.HiddenDangerEnum;
import com.budwk.app.sqfwV2.enums.centuryCityOldCare.CenturyCityOldCareV2FileEnums;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-06-06
 * <p>
 * 世纪城养老服务站巡查1.1 CenturyCityOldCare
 * 观山湖区食品销售日常巡查表.docx
 */
public class FoodSalesDailyInsPdfGenerator extends BasePdfGenerator {

    private final Map<String, HiddenDangerEnumBO> hiddenDangerEnumBOMap;

    public FoodSalesDailyInsPdfGenerator() {
        Map<String, HiddenDangerEnumBO> hiddenDangerEnumTempMap = Maps.newHashMap();
        for (HiddenDangerEnum value : HiddenDangerEnum.values()) {
            HiddenDangerEnumBO hiddenDangerEnumBO = new HiddenDangerEnumBO();
            BeanUtil.copyProperties(value, hiddenDangerEnumBO);
            hiddenDangerEnumTempMap.put(value.getCode(), hiddenDangerEnumBO);
        }
        this.hiddenDangerEnumBOMap = hiddenDangerEnumTempMap;
    }


    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        return true;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return CenturyCityOldCareV2FileEnums.foodSalesDailyInsPdf.getPath();
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        CenturyCityOldCarePdfDTO paramData = (CenturyCityOldCarePdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        Date deadlineForRectification = paramData.getDeadlineForRectification();
        if (deadlineForRectification != null) {
            Date fillDate = pdfGenerateParam.getBusinessParam().getFillDate();
            long dayBetween = DateUtil.betweenDay(fillDate, deadlineForRectification, true);
            paramMap.put("deadlineForRectification", dayBetween);
        }

        paramMap.put("transferToMarketSupervisionAndManagement", paramData.getTransferToMarketSupervisionAndManagement());
        paramMap.put("otherHandlingSuggestions", paramData.getOtherHandlingSuggestions());

        List<String> riskImageList = Lists.newArrayList();
        List<String> rectificationAfterImageList = Lists.newArrayList();

        // 使用这种方式快速的把问题和问题对应的值赋值进来
        List<CenturyCityProblemParamDTO> foodServiceFormList = Optional.ofNullable(paramData.getFoodServiceForm()).orElse(Lists.newArrayList());
        List<CenturyCityProblemParamDTO> foodSaleForm = Optional.ofNullable(paramData.getFoodSaleForm()).orElse(Lists.newArrayList());
        List<ProblemParamDTO> allSaleFormList = Lists.newArrayList();
        allSaleFormList.addAll(foodServiceFormList);
        allSaleFormList.addAll(foodSaleForm);

        for (ProblemParamDTO foodServiceItem : allSaleFormList) {
            String riskCode = foodServiceItem.getRiskCode();
            String key = foodServiceItem.getKey();
            HiddenDangerEnumBO hiddenDangerEnumBO = this.hiddenDangerEnumBOMap.get(riskCode);
            if (hiddenDangerEnumBO != null) {
                hiddenDangerEnumBO.setKey(key);
                this.hiddenDangerEnumBOMap.put(riskCode, hiddenDangerEnumBO);
            }

            if (StringUtils.isNotBlank(foodServiceItem.getRiskImages())) {
                riskImageList.addAll(Lists.newArrayList(Splitter.on(",").split(foodServiceItem.getRiskImages())));
            }
            if (StringUtils.isNotBlank(foodServiceItem.getRectificationAfterPhoto())) {
                rectificationAfterImageList.addAll(Lists.newArrayList(Splitter.on(",").split(foodServiceItem.getRectificationAfterPhoto())));
            }
        }

        // 下面是各个服务问题的汇总 接下来的代码就比较魔幻了，请参考这个在线文档，我的天我写的时候我也是崩溃的
        // https://docs.qq.com/sheet/DZGVVQ2lpQXJibWxX?tab=kcuch4
        String ser11 = "是", ser12 = "是";
        String ser21 = "是", ser22 = "是", ser23 = "是";
        String ser31 = "是", ser32 = "是", ser33 = "是", ser34 = "是", ser35 = "是", ser36 = "是", ser37 = "是";
        String ser41 = "是";
        String ser51 = "是";
        String ser61 = "是", ser62 = "是";

        ser11 = this.hiddenDangerEnumBOMap.get("45").getKey();
        ser12 = this.hiddenDangerEnumBOMap.get("55").getKey();
        ser21 = this.hiddenDangerEnumBOMap.get("55").getKey();
        ser22 = this.hiddenDangerEnumBOMap.get("50").getKey();
        ser23 = this.hiddenDangerEnumBOMap.get("57").getKey();
        ser31 = this.hiddenDangerEnumBOMap.get("58").getKey();
        ser32 = this.hiddenDangerEnumBOMap.get("59").getKey();
        ser33 = this.hiddenDangerEnumBOMap.get("60").getKey();
        ser34 = this.hiddenDangerEnumBOMap.get("61").getKey();
        ser35 = this.hiddenDangerEnumBOMap.get("62").getKey();
        ser36 = this.hiddenDangerEnumBOMap.get("63").getKey();
        ser37 = this.hiddenDangerEnumBOMap.get("64").getKey();
        ser41 = this.hiddenDangerEnumBOMap.get("48").getKey();
        ser51 = this.hiddenDangerEnumBOMap.get("65").getKey();
        ser61 = this.hiddenDangerEnumBOMap.get("89").getKey();
        ser62 = this.hiddenDangerEnumBOMap.get("90").getKey();

        paramMap.put("ser11", ser11);
        paramMap.put("ser12", ser12);
        paramMap.put("ser21", ser21);
        paramMap.put("ser22", ser22);
        paramMap.put("ser23", ser23);
        paramMap.put("ser31", ser31);
        paramMap.put("ser32", ser32);
        paramMap.put("ser33", ser33);
        paramMap.put("ser34", ser34);
        paramMap.put("ser35", ser35);
        paramMap.put("ser36", ser36);
        paramMap.put("ser37", ser37);
        paramMap.put("ser41", ser41);
        paramMap.put("ser51", ser51);
        paramMap.put("ser61", ser61);
        paramMap.put("ser62", ser62);

        String imagesDesc = "", imagesLastDesc = "";
        if (riskImageList.size() > 0) {
            imagesDesc = StrFormatter.format("{} {} 食品销售日常巡查整改前照片", paramData.getPlaceName(),
                    DateUtil.format(paramData.getInspectionDate(), "yyyy年MM月dd日"));
        }
        paramMap.put("imagesDesc", imagesDesc);
        paramMap.put("images", CommonFormOfficeUtils.addWordPicture(riskImageList, "pictures", 5f));


        if (rectificationAfterImageList.size() > 0) {
            imagesLastDesc = StrFormatter.format("{} {} 整改后照片", paramData.getPlaceName(),
                    DateUtil.format(paramData.getInspectionDate(), "yyyy年MM月dd日"));
        }

        paramMap.put("imagesLastDesc", imagesLastDesc);
        paramMap.put("imagesLast", CommonFormOfficeUtils.addWordPicture(rectificationAfterImageList, "pictures", 5f));
    }
}
